<?php
class ControllerNewsletterSubscriber extends Controller {
	private $error = array();
	
	public function index() { 
		$this->load->language('newsletter/subscriber');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('newsletter/subscriber');
		
		$this->getList();
	}
	
	public function insert() {
        $this->load->language('newsletter/subscriber');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('newsletter/subscriber');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_newsletter_subscriber->addSubscriber($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect($this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'].$url, 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
        $this->load->language('newsletter/subscriber');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('newsletter/subscriber');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_newsletter_subscriber->editSubscriber($this->request->get['subscriber_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			$this->redirect($this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'].$url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
        $this->load->language('newsletter/subscriber');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->load->model('newsletter/subscriber');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
      		foreach ($this->request->post['selected'] as $subscriber_id) {
				$this->model_newsletter_subscriber->deleteSubscriber($subscriber_id);
			}
						
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect($this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'].$url));
		}

		$this->getList();
	}
	
	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'subscriber_id';
		}
		 
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		$url = '';
	
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}	
	
  		$this->data['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('common/home', 'token='.$this->session->data['token'], 'SSL'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'].$url, 'SSL'),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = $this->url->link('newsletter/subscriber/insert', 'token='.$this->session->data['token'].$url, 'SSL');
		$this->data['delete'] = $this->url->link('newsletter/subscriber/delete', 'token='.$this->session->data['token'].$url, 'SSL');

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		$subscriber_list_total = $this->model_newsletter_subscriber->getTotalSubscribers();
		$results = $this->model_newsletter_subscriber->getSubscribers($data);
        $this->data['subscriber_list'] = array();
		foreach ($results as $result) {
			$action = array();			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('newsletter/subscriber/update', 'token='.$this->session->data['token'].'&subscriber_id='.$result['subscriber_id'].$url, 'SSL')
			);		
		
			$this->data['subscriber_list'][] = array(
				'subscriber_id' => $result['subscriber_id'],
				'name'          => $result['name'],
				'email'         => $result['email'],
				'action'        => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_id'] = $this->language->get('column_id');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_email'] = $this->language->get('column_email');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$url = '';
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		$this->data['sort_id'] = $this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'] . '&sort=subscriber_id' . $url, 'SSL');
		$this->data['sort_name'] = $this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'] . '&sort=name' . $url, 'SSL');
		$this->data['sort_email'] = $this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'] . '&sort=email' . $url, 'SSL');
		
		$url = '';
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $subscriber_list_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('newsletter/subscriber', 'token='.$this->session->data['token'].$url.'&page={page}', 'SSL');
		
		$this->data['pagination'] = $pagination->render();
		$this->data['sort'] = $sort; 
		$this->data['order'] = $order;
		
		$this->template = 'newsletter/subscriber_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
 	}
	
	private function getForm() {
		$this->data['heading_title'] = $this->language->get('entry_title');
		
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_id'] = $this->language->get('entry_id');
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_email'] = $this->language->get('entry_email');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}

		$url = '';			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

        $this->data['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('newsletter/subscriber', 'token=' . $this->session->data['token'] . $url, 'SSL'),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);

		if (!isset($this->request->get['subscriber_id'])) {
			$this->data['action'] = $this->url->link('newsletter/subscriber/insert&token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('newsletter/subscriber/update&token=' . $this->session->data['token'] . '&subscriber_id=' . $this->request->get['subscriber_id'] . $url, 'SSL');
		}		  
    	$this->data['cancel'] = $this->url->link('newsletter/subscriber&token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['subscriber_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$subscriber = $this->model_newsletter_subscriber->getSubscriber($this->request->get['subscriber_id']);
		}

		if (isset($this->request->post['name'])) {
			$this->data['name'] = $this->request->post['name'];
		} elseif (isset($subscriber)) {
			$this->data['name'] = $subscriber['name'];
		} else {
			$this->data['name'] = '';
		}
		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} elseif (isset($subscriber)) {
			$this->data['email'] = $subscriber['email'];
		} else {
			$this->data['email'] = '';
		}

		$this->template = 'newsletter/subscriber_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression')); 
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'newsletter/subscriber')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if ((strlen(utf8_decode($this->request->post['name'])) < 0) || (strlen(utf8_decode($this->request->post['name'])) > 255)) {
			$this->error['name'] = $this->language->get('error_name');
		}

        $pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';
		if ((strlen(utf8_decode($this->request->post['email'])) > 100) || (!preg_match($pattern, $this->request->post['email']))) {
			$this->error['email'] = $this->language->get('error_email');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'newsletter/subscriber')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>